package com.hr.system.controller;

import java.util.List;

import com.hr.common.core.domain.entity.SysUser;
import com.hr.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hr.common.annotation.Log;
import com.hr.common.enums.BusinessType;
import com.hr.system.domain.SysLeave;
import com.hr.system.service.ISysLeaveService;
import com.hr.common.core.controller.BaseController;
import com.hr.common.core.domain.AjaxResult;
import com.hr.common.utils.poi.ExcelUtil;
import com.hr.common.core.page.TableDataInfo;

/**
 * 请假Controller
 * 
 * @author ruoyi
 * @date 2021-03-16
 */
@Controller
@RequestMapping("/system/leave")
public class SysLeaveController extends BaseController
{
    private String prefix = "system/leave";

    @Autowired
    private ISysLeaveService sysLeaveService;

    @RequiresPermissions("system:leave:view")
    @GetMapping()
    public String leave()
    {
        return prefix + "/leave";
    }

    /**
     * 查询请假列表
     */
    @RequiresPermissions("system:leave:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysLeave sysLeave)
    {
        startPage();
        List<SysLeave> list = sysLeaveService.selectSysLeaveList(sysLeave);
        return getDataTable(list);
    }

    @RequiresPermissions("system:leave:toNeedLeave")
    @GetMapping("/toNeedLeave")
    public String toNeedLeave(){
        return prefix + "/needLeave";
    }

    @RequiresPermissions("system:leave:toApproval")
    @GetMapping("/toLeaveApproval")
    public String toApproval(){
        return prefix + "/approval";
    }

    @PostMapping("/myLeaveList")
    @ResponseBody
    public TableDataInfo iWantLeave(SysLeave sysLeave){

        SysUser sysUser = ShiroUtils.getSysUser();
        sysLeave.setUserId(sysUser.getUserId());
        sysLeave.setTenantId(Integer.toUnsignedLong(sysUser.getTenantId()));
        startPage();
        List<SysLeave> list = sysLeaveService.selectSysLeaveList(sysLeave);

        return getDataTable(list);
    }

    @PostMapping("/myDeptLeaveList")
    @ResponseBody
    public TableDataInfo myDeptLeaveList(SysLeave sysLeave){
        SysUser sysUser = ShiroUtils.getSysUser();
        sysLeave.setDeptId(sysUser.getDeptId().intValue());
        sysLeave.setLeaveStatus(1);
        sysLeave.setTenantId(Integer.toUnsignedLong(sysUser.getTenantId()));
        startPage();
        List<SysLeave> list = sysLeaveService.selectSysLeaveList(sysLeave);
        return getDataTable(list);
    }

    /**
     * 导出请假列表
     */
    @RequiresPermissions("system:leave:export")
    @Log(title = "请假", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysLeave sysLeave)
    {
        List<SysLeave> list = sysLeaveService.selectSysLeaveList(sysLeave);
        ExcelUtil<SysLeave> util = new ExcelUtil<SysLeave>(SysLeave.class);
        return util.exportExcel(list, "leave");
    }

    /**
     * 新增请假
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存请假
     */
    @RequiresPermissions("system:leave:add")
    @Log(title = "请假", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysLeave sysLeave)
    {
        int tenantId = ShiroUtils.getSysUser().getTenantId();
        Long userId = ShiroUtils.getSysUser().getUserId();
        sysLeave.setUserId(userId);
        sysLeave.setTenantId(Integer.toUnsignedLong(tenantId));
        return toAjax(sysLeaveService.insertSysLeave(sysLeave));
    }

    /**
     * 修改请假
     */
    @GetMapping("/edit/{leaveId}")
    public String edit(@PathVariable("leaveId") Long leaveId, ModelMap mmap)
    {
        SysLeave sysLeave = sysLeaveService.selectSysLeaveById(leaveId);
        mmap.put("sysLeave", sysLeave);
        return prefix + "/edit";
    }

    /**
     * 查看请假
     */
    @GetMapping("/detail/{leaveId}")
    public String view(@PathVariable("leaveId") Long leaveId, ModelMap mmap)
    {
        mmap.put("sysLeave", sysLeaveService.selectSysLeaveById(leaveId));
        return prefix + "/detail";
    }

    /**
     * 修改保存请假
     */
    @RequiresPermissions("system:leave:edit")
    @Log(title = "请假", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysLeave sysLeave)
    {
        return toAjax(sysLeaveService.updateSysLeave(sysLeave));
    }

    /**
     * 删除请假
     */
    @RequiresPermissions("system:leave:remove")
    @Log(title = "请假", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysLeaveService.deleteSysLeaveByIds(ids));
    }

    /**
     * 提交请假审批
     */
    @ResponseBody
    @PostMapping("/approval")
    public AjaxResult approval(Long id) {
        sysLeaveService.approval(id);
        return AjaxResult.success("提交审核成功");
    }

    /**
     * 领导通过请假审批
     */
    @ResponseBody
    @PostMapping("/leaderApproval")
    public AjaxResult leaderApproval(Long id) {
        sysLeaveService.leaderApproval(id);
        return AjaxResult.success("审核成功");
    }

}
